package com.artPreter.service.impl;

import com.artPreter.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Value("${server.port}")
    private String port;

    @Override
    public String uploadFile(MultipartFile file, String type) {
        String separator = File.separator;
        String serverPath = "";
        if(port!=null && port.equals("18063")){
            serverPath = System.getProperty("user.dir")+"/home/artUpLoads";
        }else{
            serverPath ="/home/artUpLoads";
        }
        serverPath = serverPath + type;
        // 年月日文件夹
        LocalDateTime now = LocalDateTime.now();
        String dayStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String[] dayArr = dayStr.split("-");
        String dateDir = separator + dayArr[0] + separator + dayArr[1] + separator + dayArr[2];
        String path = serverPath + dateDir;

        String year = dayArr[0];
        String month = dayArr[1];
        String day = dayArr[2];

        String yearDir = serverPath + File.separator + year;
        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = path;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }

        String uuid = UUID.randomUUID().toString().trim();
        String newFileName = uuid + ".png";

        try {
            file.transferTo(new File(path+separator+newFileName));
            return "/uploads"+type+"/"+year+"/"+month+"/"+day+"/"+newFileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public String uploadVideoFile(MultipartFile file, String type) {
        String separator = File.separator;
        String serverPath = "";
        if(port!=null && port.equals("18063")){
            serverPath = System.getProperty("user.dir")+"/home/artUpLoads";
        }else{
            serverPath ="/home/artUpLoads";
        }
        serverPath = serverPath + type;
        // 年月日文件夹
        LocalDateTime now = LocalDateTime.now();
        String dayStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String[] dayArr = dayStr.split("-");
        String dateDir = separator + dayArr[0] + separator + dayArr[1] + separator + dayArr[2];
        String path = serverPath + dateDir;

        String year = dayArr[0];
        String month = dayArr[1];
        String day = dayArr[2];

        String yearDir = serverPath + File.separator + year;
        File yearFile = new File(yearDir);
        if (!yearFile.exists()) {
            yearFile.mkdirs();
        }

        String monthDir = yearDir + File.separator + month;
        File monthFile = new File(monthDir);
        if (!monthFile.exists()) {
            monthFile.mkdirs();
        }

        String dayDir = path;
        File dayFile = new File(dayDir);
        if (!dayFile.exists()) {
            dayFile.mkdirs();
        }

        String uuid = UUID.randomUUID().toString().trim();
        String newFileName = uuid + ".mp4";

        try {
            file.transferTo(new File(path+separator+newFileName));
            return "/uploads"+type+"/"+year+"/"+month+"/"+day+"/"+newFileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
